package com.itheima.article.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itheima.article.service.ApArticleConfigService;
import com.itheima.common.constants.BusinessConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ApArticleConfigListener {
  @Autowired
    private ApArticleConfigService apArticleConfigService;

  @KafkaListener(topics = BusinessConstants.MqConstants.WM_NEWS_DOWN_OR_UP_TOPIC)
    public void upOrDown(ConsumerRecord<String,String> msg){
      String content = msg.value();
      log.info("文章微服同步上下架-开始: t={},p={},o={},v={}", msg.topic(), msg.partition(), msg.offset(), content);
      //1.解析消息内容
      JSONObject jsonObject = JSON.parseObject(content);
      Long articleId = jsonObject.getLong("articleId");
      Integer enable = jsonObject.getInteger("enable");
      //2.调用service做上下架处理
      apArticleConfigService.upOrDown(articleId,enable);
      log.info("文章微服同步上下架-完成: t={},p={},o={},v={}", msg.topic(), msg.partition(), msg.offset(), content);

  }
}
